#include <reg51.h>
#define uchar unsigned char
#define uint  unsigned int
sbit RS=P2^5;    //??/????
sbit RW=P2^4;    //???
sbit  E=P2^3;    //????
sbit RES = P2^0; //??
sbit PSB = P2^2; //????
//**************************************************************************************************
//????
//**************************************************************************************************
void delay(uint time)         //int????16?,??????65535            
 {
  uint  i,j;             //????i,j,?????? 
  for(i=0;i<time;i++)    //for??,??50*time?
     for(j=0;j<100;j++); //for??,??50?
 }
//**************************************************************************************************
//??
//**************************************************************************************************
void checkbusy()           
{
   RS=0;                   //??/????,?0?????    
   RW=1;                   //?/???,?1????
   E=1;                    //??
   while((P0&0x80)==0x80); //?????,??????0,???????
   E=0;                    //???? 
}
//**************************************************************************************************
//?LCD????
//**************************************************************************************************
void wcode(uchar cmdcode)
{
   checkbusy();            //??
   RS=0;                   //??/????,?0????? 
   RW=0;                   //?/???,?0????
   E=1;                    //??
   P0=cmdcode;             //????
   delay(10);              //???? 
   E=0;                    //????
}
//**************************************************************************************************
//?LCD????
//**************************************************************************************************
void wdata(uchar dispdata)
{
   checkbusy();            //??
   RS=1;                   //??/????,?1?????
   RW=0;                   //?/???,?0????
   E=1;                    //??
   P0=dispdata;            //????
   delay(10);              //???? 
   E=0;                    //????
}

//**************************************************************************************************
//LCD ???
//**************************************************************************************************
void InitLCD()
   {
   PSB=1;             //???8BIT??????
   RES=0;             //??
   delay(10);         //??
   RES=1;             //??? 
   wcode(0x30);       //???????
   wcode(0x0c);       //???(???????)
   wcode(0x01);       //????,?????????00H
   wcode(0x06);       //????????????,?????????????????	
   }  
//**************************************************************************************************
//?????????
//**************************************************************************************************
void dis(uchar x,uchar y,uchar code *s)   
{                                     //x????,y????,*s????,???????
 switch(y)                            //?????            
     { 
	  case 0: wcode(0x80+x);break;    //?1?
	  case 1: wcode(0x90+x);break;    //?2?
	  case 2: wcode(0x88+x);break;    //?3?
	  case 3: wcode(0x98+x);break;    //?4?
      default:break;
	 }
   while(*s>0)                        //????,??????
     {  
      wdata(*s);                      //???
      delay(10);                      //????
      s++;                            //????
     }
}

void dischar(uchar x,uchar y,uchar code *s)   
{                                     //x????,y????,*s????,???????
 switch(y)                            //?????            
   { 
	  case 0: wcode(0x80+x);break;    //?1?
	  case 1: wcode(0x90+x);break;    //?2?
	  case 2: wcode(0x88+x);break;    //?3?
	  case 3: wcode(0x98+x);break;    //?4?
      default:break;
	 }
   wdata(s);                      //???
   delay(10);                      //????
}
